<template>
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap topbarwrap">
      <!-- 左 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" width="23px" alt="" />
          用户名：{{userInfo.nickName}}
        </li>
        <li>我的积分：{{userInfo.coin}}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{cartTotal}}</strong>
        </li>
        <li class="btnlogin" @click="changeIsShowModal(true)" v-else>登录</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState,mapMutations,mapActions} from "vuex";
//微信扫码登录接口
import {WeiXinLoginAPI} from "@/request/api"
export default {
  // watch:{
  //   "$route.path":{
  //     handler(newVal,oldVal){
  //       console.log(newVal,oldVal);
  //       let mytoken = localStorage.getItem('x-auth-token')
  //       // if(!mytoken){
  //       //   this.changeIsLogined(true)
  //       // }else{
  //       //   this.changeIsLogined(false)
  //       // }
  //       this.changeIsLogined(Boolean(mytoken))
  //     }
  //   }
  // },
 async created(){
    //this.$route.query.code 当前路由的code
    // console.log("topbar-code",this.$route.query.code);
    let mycode = this.$route.query.code
    if(mycode){
      let res = await WeiXinLoginAPI({
        code:mycode
      })
      if(res.code ==0){
        //扫码成功提示
        this.asyncIsShowToast({
                show:true,
                msg:"登录成功",
                type:"success"
            });
        //保存token值
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 清除token
        this.$router.push(this.$route.path)
        //登录切换至购物车 更新登录状态
        this.changeIsLogined(true);
        this.asyncUserInfo()
      }else if(res.code ==400){
        //重新扫码
        //二维码失效 提示用户重新扫码
        this.asyncIsShowToast({
                show:true,
                msg:"二维码已过期,请重新扫码",
                type:"danger"
            });
          this.changeIsShowModal(true)
      }else if(res.code ==407){
        //需要用户再进行一次手机号登录
        //为用户弹出登录框
        this.asyncIsShowToast({
                show:true,
                msg:"请用手机号绑定微信",
                type:"warning"
            });
            // 帮用户打开新窗口
             this.changeIsShowModal(true)
            // 保存uuid
            localStorage.setItem("uuid",res.uuid)
      }
    }else{
      // 如果不是微信登录进来的，就会执行这里的代码
      // 组件重载
      let mytoken = localStorage.getItem("x-auth-token")
      this.changeIsLogined(Boolean(mytoken))
      if(mytoken){
        // 更新
        this.asyncUserInfo()
      }else{
        // 初始化用户信息
        this.initUserInfo()
      }
    }
  },
  computed:{
      ...mapState({
        isLogined:state => state.loginModal.isLogined,
        userInfo:state => state.userInfo.userInfo,
        cartTotal:state => state.userInfo.cartTotal
      })
  },
  methods: {
    ...mapMutations({
      changeIsShowModal: "showModal/changeIsShowModal",
      changeIsLogined:"loginModal/changeIsLogined",
      initUserInfo:"userInfo/initUserInfo"
    }),
    ...mapActions({
     asyncIsShowToast:'showToast/asyncIsShowToast',
     asyncUserInfo:'userInfo/asyncUserInfo'
    })
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background-color: #242b39;
  font-size: 14px;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
  }
  .right {
    li {
      float: left;
      margin-left: 20px;
      cursor: pointer;
      &.cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center; 
        img{
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span{
          margin: 0 8px;
        }
        strong{
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
    img {
      vertical-align: middle;
      border-radius: 50%;
    }
    .btnlogin {
      width: 124px;
      height: 40px;
      text-align: center;
      background-color: #0a328e;
    }
  }
}
</style>